import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;

/**
 * Created by Kail on 2016/9/14.
 */
public class GitReadFileByCommitId {

    /**
     * echo $refname
     * echo $oldrev
     * echo $newrev
     * <p/>
     * files=$(git diff --name-only $oldrev $newrev | xargs )
     * <p/>
     * java -jar D:/workspace/repo/jgit-learn/target/jgit-learn-1.0-SNAPSHOT.jar $files $newrev
     */
    public static void main(String[] args) throws Exception {

        args = new String[]{"", "", "", "", "", "", "", "", ""};

        // Git 项目 根目录
        String projectPath = args[0];
        // 提交上来的最新版版本的ID
        String commintId = args[1];
        // 提交上拍的源码路径
        String srcPath = args[2];

        List<String> changeFileList = Arrays.asList(args).subList(3, args.length);


//        projectPath = "D:\\workspace\\idea\\ttpai_boss";
//        commintId = "a1a5c6395fb92d2d948d9ab76b7319e291cf9205";
//        srcPath = "D:/";
//        changeFileList = new ArrayList<>();
//        changeFileList.add("src/main/java/com/ttpai/boss/common/service/BossPhoneHistoryModuleService.java");
//        changeFileList.add("src/main/java/com/ttpai/boss/successauction/controllers/BossAuctionController.java");
//        changeFileList.add("src/main/java/com/ttpai/boss/successauction/controllers/SuccessBringController.java");
//        changeFileList.add("src/main/java/com/ttpai/boss/successauction/service/BossReceptionFollowWorkService.java");
//        changeFileList.add("src/main/java/com/ttpai/boss/successauction/service/BossSuccessAuctionService.java");

        System.out.println("======== start copy file ");
        long start = System.currentTimeMillis();
        try (Repository repository = Git.open(new File(projectPath)).getRepository()) {
            RevWalk walk = new RevWalk(repository);
            ObjectId objId = repository.resolve(commintId);
            RevCommit revCommit = walk.parseCommit(objId);
            RevTree revTree = revCommit.getTree();


            List<String> betterChangeFiles = new ArrayList<>();
            List<File> betterRealSrc = new ArrayList<>();

            for (String changeFile : changeFileList) {
                // TODO 如果文件过多的话，只分析Service文件， 根据文件后缀名排序
                if (changeFile.endsWith(".java")) {
                    File file = new File(srcPath + File.separator + changeFile);
                    file.getParentFile().mkdirs();

                }
            }


//            try (ByteArrayOutputStream out = new ByteArrayOutputStream(); FileOutputStream fileOutputStream = new FileOutputStream(file)) {
//                //child表示相对git库的文件路径
//                TreeWalk treeWalk = TreeWalk.forPath(repository, changeFile, revTree);
//                ObjectLoader loader = repository.open(treeWalk.getObjectId(0));
//                loader.copyTo(out);
//                out.writeTo(fileOutputStream);
//                out.flush();
//            } catch (Exception ex) {
//                ex.printStackTrace();
//                // noting
//            }


        }
        long end = System.currentTimeMillis() - start;
        System.out.println("======== copy file end, use time : " + end + " ms");

    }

}
